//导入数据库模块
const db = require('../db/index')

//发布商品
exports.releaseGoods = (req, res) => {
    const sql = 'INSERT INTO hb_product set ?'
    const goodsInfo = { user_name: req.user.user_name, ...req.body }
    db.query(sql, goodsInfo, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('商品发布失败！')
        res.cc('商品发布成功！', 200)
    })
}

// 获取商品信息
exports.getproductInfo = (req, res) => {
    const sql = 'select * from hb_product where product_id = ?';
    db.query(sql, [req.query.product_id], (err, results) => {
        if (err) return res.cc(err);
        if (results.length !== 1) return res.cc('未找到对应商品！');
        res.send({
            status: 200,
            data: results[0]
        });
    });
};


//获取商品列表
exports.getProductList = (req, res) => {
    const sql = 'select * from hb_product'
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.cc('商品获取失败！')
        res.send({
            status: 200,
            data: results
        })
    })
}

//根据类型查商品
exports.getProductTageInfo = (req, res) => {
    const sql = 'select * from hb_product where product_tage = ?'
    db.query(sql, req.query.product_tage, (err, results) => {
        if (err) return res.cc(err)
        res.send({
            status: 200,
            data: results
        })
    })
}

//模糊查询商品
exports.fuzzySearch = (req, res) => {
    const sql = `SELECT pd.*, tg.tage_name 
    FROM hb_product pd 
    JOIN hb_tage tg 
    ON pd.product_tage = tg.product_tage 
    WHERE pd.product_name LIKE ? OR tg.tage_name LIKE ?`

    db.query(sql, ['%' + req.body.search_name + '%', '%' + req.body.search_name + '%'], (err, results) => {
        if (err) return res.cc(err)
        if (results.length === 0) return res.send({ status: 400, msg: '没有查询到商品！' })
        res.send({
            status: 200,
            data: results
        })
    })
}

//查询自己发布的商品
exports.getProductInfoBYown = (req, res) => {
    const sql = 'SELECT * FROM hb_product WHERE user_name = ? AND failure_tage <> 1';
    db.query(sql, req.user.user_name, (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 0) return res.send({ status: 200 });
        res.send({
            status: 200,
            data: results
        });
    });
}


//删除商品信息
exports.deleteProductInfo = (req, res) => {
    const sql = 'delete from hb_product where product_id = ?'
    db.query(sql, req.body.product_id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('删除失败！')
        res.cc('删除成功！', 200)
    })
}

//添加历史搜索
exports.historySearch = (req, res) => {
    const sql = 'INSERT INTO history_search SET ?'
    const historyData = { history_content: req.body.search_name };
    db.query(sql, historyData, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('添加失败！')
        res.cc('添加成功', 200)
    })
}

//获取历史搜索 
exports.gethistorySearch = (req, res) => {
    const sql = 'SELECT * FROM history_search '
    db.query(sql, (err, results) => {
        if (err) return res.cc(err)
        res.send({ status: 200, data: results })
    })
}

//标记商品失效
exports.failureTage = (req, res) => {
    const sql = `UPDATE hb_product
    SET failure_tage = '1'
    WHERE product_id = ?`
    const product_id = parseInt(req.body.product_id);
    db.query(sql, product_id, (err, results) => {
        if (err) return res.cc(err)
        if (results.affectedRows !== 1) return res.cc('删除失败！')
        res.send({ status: 200, msg: '删除成功' })
    })
}

//获取失效的商品
exports.getFailureProduct = (req, res) => {
    const sql = 'SELECT * FROM hb_product WHERE user_name = ? AND failure_tage = 1';
    db.query(sql, req.user.user_name, (err, results) => {
        if (err) return res.cc(err);
        if (results.length === 0) return res.send({ status: 200 });
        res.send({
            status: 200,
            data: results
        });
    });
}